package com.sn.frame;

import com.sn.constant.Common;
import com.sn.constant.Windows;
import com.sn.util.Capture;
import com.sn.net.Client;
import com.sn.net.Server;
import com.sn.task.UdpSendTask;
import com.sn.task.WaitConnectTask;
import com.sn.util.IPUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.InetAddress;
import java.net.URL;
import java.net.UnknownHostException;

/**
 * @Description: 首页
 * @Author: songbiao
 */
public class IndexFrame extends JFrame {


    private static final int _HEIGHT = 400;
    private static final int _WIDTH = 400;
    private static Server server;
    private static Capture capture;
    private static Client client;

    @Override
    public void paint(Graphics g) {
        URL resource = this.getClass().getResource("/image/1.png");
        ImageIcon imageIcon = new ImageIcon(resource);
        Image image = imageIcon.getImage();
        g.drawImage(image, 0, 0, _WIDTH, _HEIGHT,this);
    }


    public IndexFrame() {
        setTitle("实时投屏v1.0   @athor:songibao");
        //居中显示
        int x = Windows.DEFAULT_WIDTH/2-(_WIDTH/2);
        int y = Windows.DEFAULT_HEIGHT/2-(_HEIGHT/2);
        setBounds(x,y,_WIDTH,_HEIGHT);
        setLayout(null);

        setResizable(false);//窗体不可变

        JPanel panel = new JPanel();
        panel.setBounds(140,120,100,100);
        JButton host = new JButton("我要投屏");
        JButton connect = new JButton("我要连接");
        panel.add(host);
        panel.add(connect);
        this.add(panel);

        host.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String option = (String) JOptionPane.showInputDialog(panel, "下列是你当前的ip:\n", "查看ip", JOptionPane.PLAIN_MESSAGE, null, IPUtils.getLocalIPList().toArray(), "");
                if (option!=null) {
                    initServer();//初始化客户端
                    JOptionPane.showMessageDialog(panel,"服务端创建成功,请保持在同一局域网连接\n结束投屏关闭窗口即可");
                    host.removeActionListener(this);
                }
            }
        });
        connect.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    String choiceIp = null;
                    choiceIp = (String) JOptionPane.showInputDialog(panel, "请选择你的ip地址:\n", "选择ip", JOptionPane.PLAIN_MESSAGE, null, IPUtils.getLocalIPList().toArray(), InetAddress.getLocalHost().getHostAddress());
                    if (choiceIp != null) {
                        String toIP = JOptionPane.showInputDialog(panel,"请输入服务端主机ip地址","127.0.0.1:7758");
                        if (toIP!=null) {
                            initClient(choiceIp,toIP);
                        }
                    }
                } catch (UnknownHostException ex) {
                    ex.printStackTrace();
                }
            }
        });

        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public void initServer() {
        //初始化服务端
        server = new Server(Common.server_port);
        capture = new Capture();

        //启动线程
        new Thread(new WaitConnectTask(server)).start();
        new Thread(new UdpSendTask(capture,server)).start();
        new Thread(new UdpSendTask(capture,server)).start();
    }

    public void initClient(String localIp,String address) {
        client = new Client(localIp);
        String[] split = address.split(":");
        if (client.send(split[0], Integer.valueOf(split[1]))) {
            System.out.println("向服务端发送连接信息=====>"+address);
            if (client.start()!=null) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        client.accept();
                    }
                }).start();
            }
        }
    }

}